module IPFS.Client.Command (
  Command(..),
  defCommand,
  genQSObject
) where

import Prelude

import Data.Argonaut (Json, fromString)
import Data.Array (uncons)
import Data.Maybe (Maybe(..))
import Data.Tuple (Tuple)
import Data.Tuple.Nested ((/\))
import Effect (Effect)
import Foreign.Object as O

type Command = { path::String,
                 qs::O.Object Json,
                 args::Maybe (Array Json),
                 multipart::Maybe Boolean,
                 multipartBoundary::Maybe String,
                 stream           ::Maybe Boolean,
                 recursive        ::Maybe Boolean,
                 progress         ::Maybe (Int -> Effect Unit),
                 isBuffer         ::Boolean,
                 disableChunkJson ::Boolean
               }

defCommand::Command 
defCommand = {path:"" , 
              qs:O.empty,
              args:Nothing,
              multipart:Nothing,
              multipartBoundary:Nothing,
              stream:Nothing,
              recursive:Nothing,
              progress:Nothing,
              isBuffer:true,
              disableChunkJson:false
             }


genQSObject::Array (Tuple String (Maybe String)) -> O.Object Json -> O.Object Json
genQSObject arr  obj = case uncons  arr of
                          Nothing                 -> obj
                          Just {head: (key /\ v), tail: tarr} -> case v of
                                                               Nothing  -> genQSObject tarr obj
                                                               Just jv  -> genQSObject tarr $ O.insert key (fromString  jv) obj
                                                  